package com.emip.servlet.baseinfo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emip.bean.AreaBean;
import com.emip.pojo.Table;
import com.emip.service.AreaService;
import com.emip.util.TableFormat;

import com.emip.config.SysParams;

/**
 * 区域信息
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="/base/area")
public class AreaAction {

	@Autowired
	AreaService areaService;
	
	@RequestMapping(value="/management")
	public String index() {		
		return "/baseInfo/AreaManagement";
	}
		
	@RequestMapping(value="/getAllAreas")
	public @ResponseBody Table<AreaBean> getAllAreas() {
		List<AreaBean> result = areaService.getAll();
		int records = result.size();
		int totalPages = records / SysParams.gridDefinedRowNumber + 1;
		return new TableFormat<AreaBean>().formTable(totalPages, 1, records, result);
	}
	
	@RequestMapping(value="/getAreaByID",method=RequestMethod.POST, headers="Accept=application/json")
	public @ResponseBody AreaBean getAreaByID(@RequestParam("eid") int eid) {
		return areaService.getAreaByID(eid);
	}
	
	@RequestMapping(value="/addArea",method=RequestMethod.POST)
	public @ResponseBody AreaBean addArea(@RequestParam("eAreaID") String areaID, @RequestParam("eProvince") String province, @RequestParam("eCity") String city,
			@RequestParam("eDistrict") String district, @RequestParam("ePostCode") String postCode) {
		return areaService.addArea(areaID, province, city, district, postCode);
	}
	
	@RequestMapping(value="/updateArea",method=RequestMethod.POST)
	public @ResponseBody AreaBean updateArea(@RequestParam("eID") int id, @RequestParam("eAreaID") String areaID, @RequestParam("eProvince") String province, @RequestParam("eCity") String city,
			@RequestParam("eDistrict") String district, @RequestParam("ePostCode") String postCode) {
		return areaService.updateArea(id, areaID, province, city, district, postCode);
	}
	
	@RequestMapping(value="/deleteArea",method=RequestMethod.POST)
	public @ResponseBody String deleteArea(@RequestParam("eID") int id) {
		areaService.deleteArea(id);
		return "success";
	}
	
	@RequestMapping(value="/getAllAreasObj")
	public @ResponseBody List<AreaBean> getAllAreasObj() {
		return areaService.getAll();
	}
}
